package com.deeper.customview.ui.c4.demo.c04d08;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class Sweep extends View {
    private Paint paint;
    private Matrix matrix;
    private SweepGradient shader;
    private float rotate;


    public Sweep(Context context) {
        super(context);
    }


    public Sweep(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        matrix = new Matrix();


        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);

        shader = new SweepGradient(
                200, 200, new int[]{Color.GREEN, Color.RED, Color.YELLOW, Color.GREEN,}, null
        );
        paint.setShader(shader);
    }


    public Sweep(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        matrix.setRotate(rotate, 200, 200);
        shader.setLocalMatrix(matrix);
        rotate += 3;
        if (rotate >= 360) {
            rotate = 0;
        }
        invalidate();
        canvas.drawCircle(200, 200, 200, paint);
    }
}
